<?php
/**
 * 合作分润
 * 目前只支持标金额的分润，主要服务于昌南贷和仁润之间的分润需求
 */

hezuoFenrun();

/**
 * 读取合作分润配置，并执行配置里的函数
 */
function hezuoFenrun(){
    $hz_fr_cfg = cfg::get('hz_fr_cfg');
    if($hz_fr_cfg){
        $cfgArr = json_decode($hz_fr_cfg,true);
        foreach($cfgArr as $v){
            if($v['status']==1 && $v['callback']){
                $v['callback']($v);
            }
        }
    }
}

/**
 * 昌南贷分润计算(合作利润分享)
 * 每天执行一次，每天只能执行一次不能重复执行
 * 每天满标复审通过后按月分享利润给合作用户
 * @author CLW 2015-12-21
 */
function cndFenrun($cfg){
    if(!isset($dbselect)) {//前面没设置 数据库 则指定使用 DB3
        $dbselect   = Db::G3();
    }
    //下面3个数据可放到配置文件里
    $apr = $cfg['apr'];//分润百分比
    $site_id=$cfg['site_id'];//分润站点ID（人ID）
    $site_name=$cfg['site_name'];//分润站点（人）名称
    $timeRang = $cfg['jiesuan_period'];//结算周期，单位月
    $infoType = $cfg['type'];//对象类型 bid=标
    
    ////获取所有昨天满标复审的标的
    $yesLinux = strtotime('-1 day');
    $yesterday = date('Y-m-d',$yesLinux);//昨天
    $yesStart = $yesterday.' 00:00:00';
    $yesEnd = $yesterday.' 23:59:59';
    //查询昨天复审通过的所有标，并按月平坦
    $sql = "SELECT * FROM 05b_0base WHERE status=3 AND reverify_time>= '{$yesStart}' AND reverify_time<='{$yesEnd}'";
    $borrow = $dbselect->GetRows($sql);
    if(empty($borrow)){
        return;
    }
    foreach($borrow as $v){
        $totMonth = 0;
        $lirun=0;
        if($v['days']>0){//天标
            $totMonth = ceil($v['days']/30);
        }else{//月标
            $totMonth = $v['borrow_period'];
        }
        //$monthMoney = $v['account']/$totMonth;
        for($i=0;$i<$totMonth;$i++){
            //利润分享总表里是不是有数据了
            $prfiteLinux = strtotime("+".$i." month",$yesLinux);
            $profiteTime = date('Y-m-d',$prfiteLinux);
            $profitId = $dbselect->GetNum("SELECT id FROM 27_fr_profit WHERE rand_start_day<='{$profiteTime}' AND rand_end_day>='{$profiteTime}' AND site_id={$site_id} LIMIT 1");
            if($profitId<=0){
                $rand_start_day = date('Y-m-01',$prfiteLinux);
                $nextMonth = strtotime(date('Y-m-01',strtotime("+1 month",$prfiteLinux)));//下个月的第一天linux时间戳，
                $rand_end_day = date('Y-m-d',  strtotime("-1 day",$nextMonth));//下个月的第一天linux时间戳，减去一天就是本月的最后一天
                $profitId = $dbselect->InsertRow("INSERT INTO 27_fr_profit(rand_start_day,rand_end_day,site_id,site_name) VALUES(?,?,?,?)",array($rand_start_day,$rand_end_day,$site_id,$site_name));
            }
            $profit_money = $v['account']*$apr;
            $sqlLirun = "INSERT INTO 27_fr_profit_log (profit_id,profit_day,profit_money,total_money,apr,info_id,info_type) VALUES(?,?,?,?,?,?,?)";
            $rs = $dbselect->Exec($sqlLirun,array($profitId,$yesterday,$profit_money,$v['account'],$apr,$v['bid'],$infoType)); 
            if($rs){
                $sqlUp = "UPDATE 27_fr_profit SET tot_money=tot_money+?,profit_money=profit_money+?,last_do_time=NOW() WHERE id=?";
                $rs = $dbselect->Exec($sqlUp,array($v['account'],$profit_money,$profitId)); 
            }
        }
    }
}
?>